using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class DropDownListField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new DropDownListField();
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            if (cellType == DataControlCellType.DataCell)
            {
                if ((rowState & DataControlRowState.Edit) != 0 ||
                    (rowState & DataControlRowState.Insert) != 0)
                {
                    DropDownList ctrl = new DropDownList();
                    DataControlFieldCollection fields = null;
                    if(this.Control is GridView)
                    {
                        fields = ((GridView)this.Control).Columns;
                    }
                    else if(this.Control is DetailsView)
                    {
                        fields = ((DetailsView)this.Control).Fields;
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("Control of type '{0}' not handeled please do so....", this.Control.GetType().ToString()));
                    }

                    int fieldIndex = 0;
                    if(fields != null)
                    {
                        fieldIndex = fields.IndexOf(this);
                    }

                    ctrl.ID = string.Concat(rowIndex, "_", fieldIndex, "_ddl");
                    cell.Controls.Add(ctrl);

                    cell.DataBinding += new EventHandler(cell_DataBinding);

                    if (Required)
                    {
                        RequiredFieldValidator val = new RequiredFieldValidator();
                        val.ControlToValidate = ctrl.ID;
                        val.Display = RequiredDisplay;
                        if (!string.IsNullOrEmpty(RequiredMessage))
                        {
                            val.ErrorMessage = RequiredMessage;
                        }
                        else
                        {
                            val.ErrorMessage = "*Required";
                        }
                        if (!string.IsNullOrEmpty(RequiredValidationGroup))
                        {
                            val.ValidationGroup = RequiredValidationGroup;
                        }

                        cell.Controls.Add(val);
                    }
                }
                else
                {
                    Label lbl = new Label();
                    cell.Controls.Add(lbl);
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                }
            }
            else if (cellType == DataControlCellType.Header && !string.IsNullOrEmpty(HeaderText))
            {
                cell.Text = HeaderText;
            }
            else if (cellType == DataControlCellType.Footer && !string.IsNullOrEmpty(FooterText))
            {
                cell.Text = FooterText;
            }
        }

        private void cell_DataBinding(object sender, EventArgs e)
        {
            DataControlFieldCell cell = sender as DataControlFieldCell;
            DropDownList ctrl = cell.Controls[0] as DropDownList;
            ctrl.DataSourceID = this.DataSourceId;
            ctrl.DataTextField = this.DataTextField;
            ctrl.DataValueField = this.DataValueField;
            if (IncludeBlankOption)
            {
                ctrl.AppendDataBoundItems = true;
                ListItem tmp = new ListItem();
                tmp.Value = "";
                if (!string.IsNullOrEmpty(BlankOptionText))
                {
                    tmp.Text = BlankOptionText;
                }
                else
                {
                    tmp.Text = "";
                }
                ctrl.Items.Add(tmp);
            }
            ctrl.DataBound += new EventHandler(ctrl_DataBound);
        }

        private void ctrl_DataBound(object sender, EventArgs e)
        {
            DropDownList ctrl = sender as DropDownList;
            IDataItemContainer container = GetDataItemContainer(ctrl);
            if (container != null && container.DataItem != null)
            {

                object val = DataBinder.Eval(container.DataItem, DataField);
                if (val != null)
                {
                    ListItem tmp = ctrl.Items.FindByValue(val.ToString());
                    if (tmp != null)
                    {
                        tmp.Selected = true;
                    }
                }
            }
        }

        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            IDataItemContainer container = GetDataItemContainer(lbl);
            if (lbl != null && container != null && container.DataItem != null)
            {
                object val = null;
                if (!string.IsNullOrEmpty(DisplayDataField))
                    val = DataBinder.Eval(container.DataItem, DisplayDataField);
                else if (!string.IsNullOrEmpty(DataField))
                    val = DataBinder.Eval(container.DataItem, DataField);
                if (val != null)
                {
                    lbl.Text = val.ToString();
                }
            }
        }

        private IDataItemContainer GetDataItemContainer(Control control)
        {
            IDataItemContainer container = null;
            Control current = control;
            while (container == null && current != null && current.NamingContainer != null)
            {
                current = current.NamingContainer;
                container = current as IDataItemContainer;
            }
            return container;
        }

        private string dataField;
        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }

        private string displayDataField;
        public string DisplayDataField
        {
            get { return displayDataField; }
            set { displayDataField = value; }
        }

        private bool required;
        public bool Required
        {
            get { return required; }
            set { required = value; }
        }

        private string requiredMessage;
        public string RequiredMessage
        {
            get { return requiredMessage; }
            set { requiredMessage = value; }
        }

        private ValidatorDisplay requiredDisplay = ValidatorDisplay.Dynamic;
        public ValidatorDisplay RequiredDisplay
        {
            get { return requiredDisplay; }
            set { requiredDisplay = value; }
        }

        private string requiredValidationGroup;
        public string RequiredValidationGroup
        {
            get { return requiredValidationGroup; }
            set { requiredValidationGroup = value; }
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if (cell.Controls.Count > 0 && !string.IsNullOrEmpty(this.DataField))
            {
                DropDownList ctrl = cell.Controls[0] as DropDownList;
                if (ctrl != null)
                {
                    dictionary.Add(this.DataField, ctrl.SelectedValue);
                }
            }
        }

        private string dataSourceId;
        public string DataSourceId
        {
            get { return dataSourceId; }
            set { dataSourceId = value; }
        }

        private string dataTextField;
        public string DataTextField
        {
            get { return dataTextField; }
            set { dataTextField = value; }
        }

        private string dataValueField;
        public string DataValueField
        {
            get { return dataValueField; }
            set { dataValueField = value; }
        }

        private bool includeBlankOption;
        public bool IncludeBlankOption
        {
            get { return includeBlankOption; }
            set { includeBlankOption = value; }
        }

        private string blankOptionText;
        public string BlankOptionText
        {
            get { return blankOptionText; }
            set 
            {
                if (!string.IsNullOrEmpty(value))
                    includeBlankOption = true;
                blankOptionText = value; 
            }
        }
	
	
    }
}
